﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows;

namespace Sparkle.UI
{
    public class MetaControl : StackPanel, IDisposable
    {
        public FrameworkElement DataControl { get; set; }

        public TextBlock LabelControl { get; set; }

        public ToolTip InfoTooltip { get; set; }

        public string MetaDataName { get; set; }

        public MetaControl(TextBlock label, Control control)
            : base()
        {
            this.Children.Add(label);
            this.Children.Add(control);
            LabelControl = label;
            DataControl = control;
        }
        public MetaControl(TextBlock label, TextBlock control)
            : base()
        {
            this.Children.Add(label);
            this.Children.Add(control);
            LabelControl = label;
            DataControl = control;
        }
        public MetaControl(Control control)
            : base()
        {
            this.Children.Add(control);
            DataControl = control;
        }


        public MetaControl()
        {
            this.SizeChanged += new SizeChangedEventHandler(MetaControl_SizeChanged);
            Orientation = Orientation.Horizontal;
        }

        public void AddControls(TextBlock label, Control control)
        {
            this.Children.Add(label);
            this.Children.Add(control);
            LabelControl = label;
            DataControl = control;
        }

        public void AddControls(TextBlock label, TextBlock control)
        {
            this.Children.Add(label);
            this.Children.Add(control);
            LabelControl = label;
            DataControl = control;
        }

        void MetaControl_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            if (DataControl != null && DataControl is IFullWidth)
            {
                DataControl.Width = this.Width - (LabelControl == null ? 0 : LabelControl.Width);
            }
        }

        public new double Width
        {
            get { return base.Width; }
            set
            {
                base.Width = value;
                MetaControl_SizeChanged(this, null);
            }
        }

        public new object Tag
        {
            get { return base.Tag; }
            set
            {
                base.Tag = value;
                if (value != null && value is IClientValidate)
                {
                    (value as IClientValidate).ClientValidate = new ClientValidateDelegate(clientValidate);
                }
            }
        }

        private void clientValidate(bool valid, string errorMessage)
        {
            if (valid)
            {
                if (DataControl != null && (DataControl is Control))
                {
                    (DataControl as Control).ClearErrorState();
                }
            }
            else
            {
                if (DataControl != null && (DataControl is Control))
                {
                    (DataControl as Control).SetErrorState(errorMessage);
                }
            }
        }

        private void setInfoOn(string infoMessage)
        {
            if (InfoTooltip == null)
            {
                InfoTooltip = new ToolTip();
            }
            InfoTooltip.PlacementTarget = DataControl;
            InfoTooltip.Content = infoMessage;
            InfoTooltip.IsOpen = true;
        }

        private void setInfoOff()
        {
            if (InfoTooltip != null)
            {
                InfoTooltip.IsOpen = false;
                //ErrorTooltip.PlacementTarget = null;
            }
        }

        /// <summary>
        /// Gets or sets a value indicating whether this instance is full width.
        /// </summary>
        /// <value>
        /// 	<c>true</c> if this instance is full width; otherwise, <c>false</c>.
        /// </value>
        public bool IsFullWidth
        {
            get
            {
                foreach (var child in this.Children)
                {
                    if (child is IFullWidth)
                    { return true; }
                }
                return false;
            }
        }


        #region IDisposable Members

        public void Dispose()
        {
            this.Children.Clear();
        }

        #endregion
    }
}
